import { createRouter, createWebHashHistory } from "vue-router";

let routes = [
  {
    path: "/",
    redirect: "/main",
  },
  {
    path: "/login",
    component: () => import("../view/login.vue"),
  },
  {
    path: "/main",
    name: "main",
    redirect: "/welcome",
    component: () => import("../view/main.vue"),
    children: [
      { path: "/welcome", component: () => import("../view/welcome.vue") },
      {
        path: "/goods/goods-list",
        component: () => import("../view/goodsList.vue"),
      },
      {
        path: "/goods/goods-info",
        component: () => import("../view/goodsInfo.vue"),
      },
      {
        path: "/user/user-list",
        component: () => import("../view/userList.vue"),
      },
      {
        path: "/user/user-setting",
        component: () => import("../view/userSetting.vue"),
      },
    ],
  },
  {
    path: "/:path(.*)",
    component: () => import("../view/fount.vue"),
  },
];

let router = createRouter({
  history: createWebHashHistory(),
  routes,
});
router.beforeEach((to, from, next) => {
  if (to.path === "/login") return next();
  const tokenStr = window.sessionStorage.getItem("taken");
  if (!tokenStr) return next("/login");
  next();
});
export default router;
